package com.dalor.phonesmanager.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.dalor.phonesmanager.R;
import com.dalor.phonesmanager.adapter.PhonesPagersAdapter;
import com.dalor.phonesmanager.bean.Image;
import com.dalor.phonesmanager.interfaces.OnPhoneItemLongClickListener;

import java.util.ArrayList;

/**
 * 作者： 吴昶 .
 * 时间: 2019/1/2 9:39
 * 功能简介：viewPager 展示图片 （以 3 页展示所有的图片）
 */
public class PagersPhonesActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private PhonesPagersAdapter adapter;
    private ArrayList<Image> phones;
    private ArrayList<Image> adapterData=new ArrayList<Image>();
    private int index;
    private boolean isScrolled=false;
    private Handler handler=new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phones_pagers);
        viewPager=(ViewPager)findViewById(R.id.vp_phones);
        initData();
    }

    private void initData(){
        phones=getIntent().getParcelableArrayListExtra("phones");
        index=getIntent().getIntExtra("index",0);
        initAdapterData();
        adapter=new PhonesPagersAdapter(this,adapterData,longClickListener);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(1,false);
        initEvent();
    }

    private void initEvent(){
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                isScrolled=true;
            }

            @Override
            public void onPageSelected(final int position) {
                if(isScrolled){
                    handler.postAtTime(new Runnable() {
                        @Override
                        public void run() {
                            resetData(position);
                        }
                    },100);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 重新设置adapter数据
     * @param position
     */
    private void resetData(int position){
        isScrolled=false;
        int scrTo=position-1+index;
        if(scrTo>=phones.size()||scrTo<0){
            viewPager.setCurrentItem(1,true);
        }else{
            index=scrTo;
            changeAdapterData(position);
            viewPager.setCurrentItem(1,false);
            adapter.notifyDataSetChanged();
        }

    }

    /**
     * 根据 index 以及 滑动的方向 变换列表的数据
     * @param position
     */
    private void changeAdapterData(int position){
        if(index==0){
            adapterData.remove(2);
            adapterData.add(0,new Image("","",0L));
        }else if(index==phones.size()-1){
            adapterData.remove(0);
            adapterData.add(new Image("","",0L));
        }else {
            if(position==0){
                adapterData.remove(2);
                adapterData.add(0,phones.get(index-1));
            }else if(position==2){
                adapterData.remove(0);
                adapterData.add(phones.get(index+1));
            }
        }
    }

    /**
     * 初始化列表的数据
     */
    private void initAdapterData(){
        adapterData.clear();
        if(index>=1){
            adapterData.add(phones.get(index-1));
        }else {
            adapterData.add(new Image("","",0L));
        }
        adapterData.add(phones.get(index));
        if(index<=phones.size()-2){
            adapterData.add(phones.get(index+1));
        }else {
            adapterData.add(new Image("","",0L));
        }
    }

    /**
     * item 长按监听
     */
    private OnPhoneItemLongClickListener longClickListener=new OnPhoneItemLongClickListener() {
        @Override
        public void onItemLongClick(String path, int position) {
            Log.d("******", "onItemLongClick: path="+path+"; position="+position);
        }
    };

}
